<template>
  <div class="login-container">
    <!-- 左侧品牌展示区 -->
    <div class="brand-display">
      <!-- 品牌Logo和标语 -->
      <div class="brand-logo">
        <img src="@/assets/logo.png" alt="Logo">
        <p>新之星管理系统</p>
      </div>
      <!-- 简短的品牌介绍文案 -->
      <p class="brand-intro">专业的赛事管理·智能数据分析·实时直播控制</p>
    </div>
    <!-- 右侧登录表单区 -->
    <div class="login-form-area">
      <!-- 欢迎登录大标题 -->
      <h1 class="welcome-title">欢迎登录</h1>
      <h3 class="welcome-msg">请使用您的账号密码登录系统</h3>

      <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form" autocomplete="on"
        label-position="left">
        <el-form-item prop="username">
          <span class="svg-container">
            <svg-icon icon-class="user" />
          </span>
          <el-input ref="username" v-model="loginForm.username" placeholder="请输入登录账号" name="username" type="text"
            tabindex="1" autocomplete="on" @focus="handleInputFocus" @blur="handleInputBlur" />
        </el-form-item>
        <el-tooltip v-model="capsTooltip" content="Caps lock is On" placement="right" manual>
          <el-form-item prop="password">
            <span class="svg-container">
              <svg-icon icon-class="password" />
            </span>
            <el-input  :key="passwordType" ref="password" v-model="loginForm.password" :type="passwordType"
              placeholder="请输入密码" name="password" tabindex="2" autocomplete="on" @keyup.native="checkCapslock"
              @keyup.enter.native="handleLogin" @focus="handleInputFocus" @blur="handleInputBlur" :show-password="false" />
            <span class="show-pwd" @click="showPwd">
              <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'" />
            </span>
          </el-form-item>
        </el-tooltip>
        
        <!-- 记住密码选项 -->
       
        <div class="additional-login-options">
          <el-checkbox style="color: #ffffff" v-model="rememberMe">记住密码</el-checkbox>
          <a style="font-size: 14px;" href="#" @click="lxkf">忘记密码</a>
          <!-- 其他登录方式图标 -->
          <!-- <img src="wecom-icon-url" alt="WeCom Login"> -->
        </div>
        <el-button :loading="loading" type="primary" style="width:100%;margin-bottom:30px;margin-top:10px;"
          @click.native.prevent="handleLogin" @mouseenter="handleButtonHover"
          @mouseleave="handleButtonLeave">登录</el-button>
        <!-- 底部附加登录选项 -->
       
      </el-form>
    </div>
  </div>
</template>

<script>
import { validUsername } from '@/utils/validate'
import SocialSign from './components/SocialSignin'

export default {
  name: 'Login',
  components: { SocialSign },
  data() {
    const validateUsername = (rule, value, callback) => {
      if (!validUsername(value)) {
        callback(new Error('请输入账号'))
      } else {
        callback()
      }
    }
    const validatePassword = (rule, value, callback) => {
      if (value.length < 6) {
        callback(new Error('密码不能低于6个字符'))
      } else {
        callback()
      }
    }
    return {
      loginForm: {
        username: '',
        password: ''
      },
      loginRules: {
        username: [{ required: true, trigger: 'blur', validator: validateUsername }],
        password: [{ required: true, trigger: 'blur', validator: validatePassword }]
      },
      passwordType: 'password',
      capsTooltip: false,
      loading: false,
      showDialog: false,
      redirect: undefined,
      otherQuery: {},
      rememberMe: false
    }
  },
  watch: {
    $route: {
      handler: function (route) {
        const query = route.query
        if (query) {
          this.redirect = query.redirect
          this.otherQuery = this.getOtherQuery(query)
        }
      },
      immediate: true
    }
  },
  created() {
    // window.addEventListener('storage', this.afterQRScan)
  },
  mounted() {
    if (localStorage.getItem('myPassword')) {
      this.loginForm.password = localStorage.getItem('myPassword')
    }
    if (this.loginForm.username === '') {
      this.$refs.username.focus()
    } else if (this.loginForm.password === '') {
      this.$refs.password.focus()
    }
  },
  destroyed() {
    // window.removeEventListener('storage', this.afterQRScan)
  },
  methods: {
    lxkf() {
      this.$message.error('联系开发,重新修改密码')
    },
    checkCapslock(e) {
      const { key } = e
      this.capsTooltip = key && key.length === 1 && (key >= 'A' && key <= 'Z')
    },
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true
          this.$store.dispatch('user/login', this.loginForm)
            .then(() => {
              this.$router.push({ path: this.redirect || '/', query: this.otherQuery })
              this.loading = false
              if (this.rememberMe) {
                localStorage.setItem('myPassword',this.loginForm.password)
              } else {
                localStorage.removeItem('myPassword')

              }
            })
            .catch(() => {
              this.loading = false
            })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    getOtherQuery(query) {
      return Object.keys(query).reduce((acc, cur) => {
        if (cur !== 'redirect') {
          acc[cur] = query[cur]
        }
        return acc
      }, {})
    },
    handleInputFocus(event) {
      event.target.classList.add('input-focus')
    },
    handleInputBlur(event) {
      event.target.classList.remove('input-focus')
    },
    handleButtonHover(event) {
      event.target.classList.add('button-hover')
    },
    handleButtonLeave(event) {
      event.target.classList.remove('button-hover')
    }
    // afterQRScan() {
    //   if (e.key === 'x-admin-oauth-code') {
    //     const code = getQueryObject(e.newValue)
    //     const codeMap = {
    //       wechat: 'code',
    //       tencent: 'code'
    //     }
    //     const type = codeMap[this.auth_type]
    //     const codeName = code[type]
    //     if (codeName) {
    //       this.$store.dispatch('LoginByThirdparty', codeName).then(() => {
    //         this.$router.push({ path: this.redirect || '/' })
    //       })
    //     } else {
    //       alert('第三方登录失败')
    //     }
    //   }
    // }
  }
}
</script>

<style lang="scss" scoped>
::v-deep .el-form-item__content {
  display: flex;
  align-items: center;
  border: 2px solid #D8D8D8;
  padding: 0px 10px;
  border-radius: 10px;
  width: 320px;
  background-color: #FFFFFF;
}
::v-deep .el-input {
  margin: 0px 10px;
  border: none;
  outline: none;
  // width: 250px;
}
::v-deep .el-input__inner:focus {
  border: none !important;
  // border-color: transparent !important;
  
  
}

::v-deep .el-input__inner {
  border: none !important;
  border-color: transparent !important;
}
::v-deep .input-focus {
  border: none !important;
  outline: none;
  border-color: transparent !important;
}
input {
  border: none !important;
  outline: none;
}
/* 整体布局 */
.login-container {
  display: flex;
  height: 100vh;
  width: 100vw;
  background-color: #223285;
}

/* 左侧品牌展示区 */
.brand-display {
  width: 30%;
  // background: linear-gradient(to bottom, #00008B, #800080);
  background-image: url(../../assets/login-bgc.png);
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  padding: 20px;
  color: #FFFFFF;
  background-size: cover;
}

.brand-logo {
  text-align: center;
  margin-bottom: 20px;
  font-size: 30px;
}

.brand-logo>img {
  width: 100px;
  height: 100px;
  border-radius: 50%;
}

.brand-intro {
  margin-top: 20px;
  text-align: center;
}

/* 右侧登录表单区 */
.login-form-area {
  width: 70%;
  background-color: #FFFFFF;
  display: flex;
  flex-direction: column;
  align-items: center;
  padding-top: 200px;
  // background-image: ;
  background-image: linear-gradient(to bottom,rgba( 28, 53,140, 0.6) 30%,rgba(  68, 28,135,.8)), url(../../assets/img.png);
  background-size: cover;
  background-position: center;
  // justify-content: center;
}

.welcome-title {
  margin-bottom: 20px;
  color: #FFFFFF;
}

.login-form {
  width: 320px;
}

/* 输入框样式 */
.el-input input {
  border: 1px solid #E8E8E8;
  border-radius: 4px;
  color: #333333;
}

.input-focus {
  // box-shadow: 0 0 5px rgba(24, 144, 255, 0.5);
}

/* 按钮样式 */
.el-button {
  background-color: #1890FF;
  border: none;
}

.button-hover {
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
}

/* 底部附加登录选项 */
.additional-login-options {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.additional-login-options a {
  color: #1890FF;
}

.welcome-msg {
  margin: 30px;
  color: #FFFFFF;
}

/* 移动端适配 */
@media screen and (max-width: 500px) {
  .login-container {
    flex-direction: column;
    background-image: url(../../assets/login-bgc.png);
    background-size: cover;
  }

  .brand-display {
    width: 100%;
    height: 200px;
    padding: 10px;
    // background-size: ccontain;
    background: none;
  }

  .brand-logo > img {
    width: 60px;
    height: 60px;
  }

  .brand-logo > p {
    font-size: 20px;
  }

  .brand-intro {
    font-size: 14px;
    margin-top: 10px;
  }

  .login-form-area {
    width: 100%;
    padding-top: 50px;
    background-image: none;
    // background-color: #21318B;
    background: none;
  }

  .welcome-title {
    font-size: 24px;
  }

  .welcome-msg {
    font-size: 14px;
    margin: 15px;
  }

  ::v-deep .el-form-item__content {
    width: 280px;
  }

  .login-form {
    width: 280px;
  }

  .additional-login-options {
    font-size: 12px;
  }
}
</style>